package com.china08.yxyapi.service.sws.teaching.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.service.sws.teaching.HomeworkStatisticsService;
import com.china08.yxyapi.util.DateUtils;
import com.google.gson.Gson;

@Service
public class HomeworkStatisticsServiceImpl implements HomeworkStatisticsService {

    private static final Logger LOGGER = Logger.getLogger(HomeworkStatisticsServiceImpl.class);

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RestTemplate restTemplate;

    // homework——class
    @Override
    public void getHomeworkForClass(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("predictDate").gte(startDate).lte(endDate);

        // 获取考试信息
        TypedAggregation<HomeworkTaskClass> aggregation = Aggregation.newAggregation(HomeworkTaskClass.class,
                Aggregation.match(criteria),
                Aggregation.group("classId")//
                        .count().as("count")//
                        .sum("numOfStudent").as("numOfStudent")//
                        .sum("submitOfStudent").as("submitOfStudent")//
                        .first("classId").as("classId")//
                        .first("className").as("className")//
                        .first("grade").as("grade")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<ClassHomeworkModel> result = mongoTemplate.aggregate(aggregation, ClassHomeworkModel.class);
        List<ClassHomeworkModel> classHomeworklLst = result.getMappedResults();
        List<ClassHomeworkModel> sendDataLst = new ArrayList<ClassHomeworkModel>();

        Integer index = 0;
        for (ClassHomeworkModel classHomeworkModel : classHomeworklLst) {

            sendDataLst.add(classHomeworkModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"classHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teachingclass/homework", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getHomeworkForClass方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getHomeworkForClass方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<ClassHomeworkModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"classHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/teachingclass/homework", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getHomeworkForClass方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getHomeworkForClass方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class ClassHomeworkModel {
        public Long count;
        public Long numOfStudent;
        public Long submitOfStudent;
        public String classId;
        public String className;
        public String grade;
        public String schoolId;
    }

    // homework——School
    @Override
    public void getHomeworkForSchool(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("predictDate").gte(startDate).lte(endDate);

        // 获取考试信息
        TypedAggregation<HomeworkTaskClass> aggregation = Aggregation.newAggregation(HomeworkTaskClass.class,
                Aggregation.match(criteria),
                Aggregation.group("schoolId")//
                        .count().as("count")//
                        .sum("numOfStudent").as("numOfStudent")//
                        .sum("submitOfStudent").as("submitOfStudent")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<SchoolHomeworkModel> result = mongoTemplate.aggregate(aggregation,
                SchoolHomeworkModel.class);
        List<SchoolHomeworkModel> schoolModelLst = result.getMappedResults();
        List<SchoolHomeworkModel> sendDataLst = new ArrayList<SchoolHomeworkModel>();

        Integer index = 0;
        for (SchoolHomeworkModel schoolModel : schoolModelLst) {

            sendDataLst.add(schoolModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"schoolHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/bureau/homework", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getHomeworkForSchool方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getHomeworkForSchool方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SchoolHomeworkModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"schoolHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/bureau/homework", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getHomeworkForSchool方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getHomeworkForSchool方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class SchoolHomeworkModel {
        public Long count;
        public Long numOfStudent;
        public Long submitOfStudent;
        public String schoolId;
    }

    // homework——Subject
    @Override
    public void getHomeworkForSubjectAndStage(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("predictDate").gte(startDate).lte(endDate);

        // 获取考试信息
        TypedAggregation<HomeworkTaskClass> aggregation = Aggregation.newAggregation(HomeworkTaskClass.class,
                Aggregation.match(criteria),
                Aggregation.group("stage", "subject", "schoolId")//
                        .count().as("count")//
                        .sum("numOfStudent").as("numOfStudent")//
                        .sum("submitOfStudent").as("submitOfStudent")//
                        .first("stage").as("stage")//
                        .first("subject").as("subject")//
                        .first("subjectName").as("subjectName")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<SubjectAndStageHomeworkModel> result = mongoTemplate.aggregate(aggregation,
                SubjectAndStageHomeworkModel.class);
        List<SubjectAndStageHomeworkModel> subjectAndStageHomeworkModellLst = result.getMappedResults();
        List<SubjectAndStageHomeworkModel> sendDataLst = new ArrayList<SubjectAndStageHomeworkModel>();

        Integer index = 0;
        for (SubjectAndStageHomeworkModel subjectAndStageHomeworkModell : subjectAndStageHomeworkModellLst) {

            sendDataLst.add(subjectAndStageHomeworkModell);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"subjectAndStageHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/schoolstagesubject/homework", formEntity,
                            String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getHomeworkForForSubjectAndStage方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getHomeworkForForSubjectAndStage方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SubjectAndStageHomeworkModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"subjectAndStageHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/schoolstagesubject/homework", formEntity,
                        String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getHomeworkForForSubjectAndStage方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getHomeworkForForSubjectAndStage方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class SubjectAndStageHomeworkModel {
        public Long count;
        public Long numOfStudent;
        public Long submitOfStudent;
        public String schoolId;
        public String stage;
        public String subject;
        public String subjectName;
    }

    // homework——user
    @Override
    public void getHomeworkForUser(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("predictDate").gte(startDate).lte(endDate);

        // 获取考试信息
        TypedAggregation<HomeworkTaskClass> aggregation = Aggregation.newAggregation(HomeworkTaskClass.class,
                Aggregation.match(criteria),
                Aggregation.group("publishUserId", "schoolId")//
                        .count().as("count")//
                        .sum("numOfStudent").as("numOfStudent")//
                        .sum("submitOfStudent").as("submitOfStudent")//
                        .first("publishUserId").as("publishUserId")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<UserHomeworkModel> result = mongoTemplate.aggregate(aggregation, UserHomeworkModel.class);
        List<UserHomeworkModel> userHomeworkModelLst = result.getMappedResults();
        List<UserHomeworkModel> sendDataLst = new ArrayList<UserHomeworkModel>();

        Integer index = 0;
        for (UserHomeworkModel userHomeworkModel : userHomeworkModelLst) {

            sendDataLst.add(userHomeworkModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"userHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teacher/homework", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getHomeworkForForUser方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getHomeworkForForUser方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<UserHomeworkModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"userHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/teacher/homework", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getHomeworkForForUser方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getHomeworkForForUser方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class UserHomeworkModel {
        public Long count;
        public Long numOfStudent;
        public Long submitOfStudent;
        public String schoolId;
        public String publishUserId;
    }

    // homework——Source(Lecture)
    @Override
    public void getHomeworkForLectureSource(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("predictDate").gte(startDate).lte(endDate);
        criteria.and("source").is(1);

        // 获取考试信息
        TypedAggregation<HomeworkTaskClass> aggregation = Aggregation.newAggregation(HomeworkTaskClass.class,
                Aggregation.match(criteria),
                Aggregation.group("stage", "subject", "schoolId")//
                        .count().as("count")//
                        .first("stage").as("stage")//
                        .first("subject").as("subjectId")//
                        .first("subjectName").as("subjectName")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<SourceHomeworkModel> result = mongoTemplate.aggregate(aggregation,
                SourceHomeworkModel.class);
        List<SourceHomeworkModel> sourceHomeworkModellLst = result.getMappedResults();
        List<SourceHomeworkModel> sendDataLst = new ArrayList<SourceHomeworkModel>();

        Integer index = 0;
        for (SourceHomeworkModel sourceHomeworkModell : sourceHomeworkModellLst) {
            sendDataLst.add(sourceHomeworkModell);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"sourceHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"Lecture\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/schoolsourcesubject", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getHomeworkForForLectureSource方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getHomeworkForForLectureSource方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SourceHomeworkModel>();
                index = 0;
            }

        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"sourceHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"Lecture\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/schoolsourcesubject", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getHomeworkForForLectureSource方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getHomeworkForForLectureSource方法执行错误：" + e.getMessage());
            }
        }
    }

    // homework——Source(Homework)
    @Override
    public void getHomeworkForHomeworkSource(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("predictDate").gte(startDate).lte(endDate);
        criteria.and("source").is(0);

        // 获取考试信息
        TypedAggregation<HomeworkTaskClass> aggregation = Aggregation.newAggregation(HomeworkTaskClass.class,
                Aggregation.match(criteria),
                Aggregation.group("stage", "subject", "schoolId")//
                        .count().as("count")//
                        .first("stage").as("stage")//
                        .first("subject").as("subjectId")//
                        .first("subjectName").as("subjectName")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<SourceHomeworkModel> result = mongoTemplate.aggregate(aggregation,
                SourceHomeworkModel.class);
        List<SourceHomeworkModel> sourceHomeworkModellLst = result.getMappedResults();
        List<SourceHomeworkModel> sendDataLst = new ArrayList<SourceHomeworkModel>();

        Integer index = 0;
        for (SourceHomeworkModel sourceHomeworkModell : sourceHomeworkModellLst) {

            sendDataLst.add(sourceHomeworkModell);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"sourceHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"Homework\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/schoolsourcesubject", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getHomeworkForForHomeworkSource方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getHomeworkForForHomeworkSource方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SourceHomeworkModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"sourceHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"Homework\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/schoolsourcesubject", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getHomeworkForForHomeworkSource方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getHomeworkForForHomeworkSource方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class SourceHomeworkModel {
        public Long count;
        public String schoolId;
        public String stage;
        public String subjectId;
        public String subjectName;
    }

    // homework——TeacherAndStage
    @Override
    public void getHomeworkForTeacherAndStage(Integer day) throws ParseException {
        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("predictDate").gte(startDate).lte(endDate);

        // 获取考试信息
        TypedAggregation<HomeworkTaskClass> aggregation = Aggregation.newAggregation(HomeworkTaskClass.class,
                Aggregation.match(criteria),
                Aggregation.group("stage", "publishUserId", "schoolId")//
                        .count().as("count")//
                        .first("stage").as("stage")//
                        .first("publishUserId").as("publishUserId")//
                        .first("publishUserNick").as("publishUserNick")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<TeacherAndStageHomeworkModel> result = mongoTemplate.aggregate(aggregation,
                TeacherAndStageHomeworkModel.class);
        List<TeacherAndStageHomeworkModel> teacherAndStageHomeworkModellLst = result.getMappedResults();
        List<TeacherAndStageHomeworkModel> sendDataLst = new ArrayList<TeacherAndStageHomeworkModel>();

        Integer index = 0;
        for (TeacherAndStageHomeworkModel teacherAndStageHomeworkModel : teacherAndStageHomeworkModellLst) {

            sendDataLst.add(teacherAndStageHomeworkModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"teacherAndStageHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teastageschool", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getHomeworkForTeacherAndStage方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getHomeworkForTeacherAndStage方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<TeacherAndStageHomeworkModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"teacherAndStageHomeworkModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/comm/school/sws/teaching/teastageschool",
                        formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getHomeworkForTeacherAndStage方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getHomeworkForTeacherAndStage方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class TeacherAndStageHomeworkModel {
        public Long count;
        public String schoolId;
        public String stage;
        public String publishUserId;
        public String publishUserNick;
    }

}
